<template>
  <div>
    <el-form :model="ruleForm"  ref="ruleForm" label-width="100px"  size="mini" class="demo-ruleForm">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="man">男</el-radio>
          <el-radio label="woman">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号" prop="telephone">
        <el-input v-model="ruleForm.telephone" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <el-form-item label="活动参加" prop="isJoin">
        <el-switch active-text="是" inactive-text="否" v-model="ruleForm.isJoin" @change="switchChange"></el-switch>
      </el-form-item>
      <el-form-item v-if="show" label="活动" prop="active">
        <el-select v-model="ruleForm.change.active" placeholder="请选择活动">
          <el-option label="田径" value="tianjing"></el-option>
          <el-option label="跳高" value="tiaogao"></el-option>
          <el-option label="跳远" value="tiaoyuan"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="show" label="时间" prop="time">
        <el-date-picker v-model="ruleForm.change.time" type="datetimerange"  range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd hh:mm:ss">

        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="show" label="参选年级" prop="grade">
        <el-checkbox-group v-model="ruleForm.change.grade">
            <el-checkbox label="seven">初一</el-checkbox>
            <el-checkbox label="eight">初二</el-checkbox>
            <el-checkbox label="nine">初三</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          ref="upload"
          :show-file-list="false"
          :on-change="handleChange"
          :on-preview="handlePreview"
          :on-error="handleError"
          :on-success="handleAvatarSuccess"
          :auto-upload="false"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  const change ={
        active: '',
        time:['2018-09-15 13:12:50', '2018-10-11 18:10:13'],
        grade:[],
  }
  export default {
    name: "upload",
    data(){
      return{
        ruleForm:{
          name: '',
          sex: '',
          telephone: '',
          isJoin: false,
        },
        show:false,
        imageUrl: '',
        fileList:[],
      }
    },
    methods:{
      switchChange(value){
        value ? (this.show = true, this.$set(this.ruleForm,'change',change)) : (this.show = false , this.$delete(this.ruleForm,'change'));
      },
      submitForm(dataform){
        this.$refs.upload.submit();
        console.log(this.ruleForm)
      },
      resetForm(dataform){
        this.$refs.upload.clearFiles();
        this.$refs[dataform].resetFields();
      },
      handleChange(file, fileList){
        // fileList = fileList[fileList.length - 1];
        fileList = fileList.slice(-1);
        this.imageUrl = URL.createObjectURL(fileList[0].raw);
      },
      handleAvatarSuccess(res, file) {
        this.$message.success('上传成功')
        console.log('上传成功')
      },
      handlePreview(file){
        console.log(file)
      },
      beforeAvatarUpload(file) {
        console.log('beforeAvatarUpload')
        console.log(file)
      },
      handleError(err,res,file){
        console.log(err)
        console.log('上传失败')
        this.$message.error('上传失败')
      }
    }

  }
</script>

<style>
.demo-ruleForm{
  width:500px;
  margin:0 auto;
  padding:50px 20px;
  border:1px solid #999;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
